﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace QuickOpen
{
    public partial class DBAssistant : Form
    {
        public DBAssistant()
        {
            InitializeComponent();
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            try
            {

                var iptTxt = rtbInput.Text;
                var splitArray = iptTxt.Split('\n');
                //var count = Math.Round( splitArray.Length*1.0 / 2,MidpointRounding.AwayFromZero);

                var columns = splitArray[0].Split('\t');
                var values = splitArray[1].Split('\t');
                //var count = columns.Length;

                var columnsSb = new StringBuilder();
                var valueSb = new StringBuilder();
                var i = 0;
                //列
                for (i = 0; i < columns.Length; i++)
                {
                    columnsSb.Append(columns[i]);
                    columnsSb.Append(",");
                }
                columnsSb.Remove(columnsSb.Length - 1, 1);
                //值
                for (i = 0; i < values.Length; i++)
                {
                    valueSb.Append("'" + values[i] + "'");
                    valueSb.Append(",");
                }
                valueSb.Remove(valueSb.Length - 1, 1);

                this.rtbResult.Text = $"INSERT INTO TABLENAME({columnsSb.ToString()}) VALUES({valueSb})";

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnWordCount_Click(object sender, EventArgs e)
        {
            this.rtbResult.Text = string.Empty;
            var txt = rtbInput.Text;
           
            
            string[] mm = Regex.Split(txt, this.txtBoxSplitReg.Text, RegexOptions.IgnoreCase);
            var wordCountDic = new Dictionary<string, int>();
            //是否忽略大小写
            if (this.cbIgnoreCase.Checked)
            {
                wordCountDic= new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
            }

            for (int i = 0; i < mm.Length; i++)
            {
                if (wordCountDic.ContainsKey(mm[i]))
                {
                    wordCountDic[mm[i]] += 1;
                }
                else
                {
                    wordCountDic.Add(mm[i], 1);
                }
            }

            var result = wordCountDic.OrderByDescending(k => k.Value);
            foreach(var item in result)
            {
                this.rtbResult.Text += $"{item.Key}\t {item.Value} \n";
            }
        }

        private void txtBoxSplitReg_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
